import { createRouter, createWebHistory } from 'vue-router'
import { constantRouse } from './routers'
import { checkLoginStatus } from '@/api/http'
import { ElMessage } from 'element-plus'

const router = createRouter({
    history: createWebHistory(),
    routes: constantRouse,
    //滚动行为
    scrollBehavior: () => ({ left: 0, top: 0 })
})
// 添加全局前置守卫
router.beforeEach(async (to, from, next) => {
  // 如果是前往登录页面，直接放行
  if (to.path === '/login' || from.path === '/login') {
    next()
    return
  }


  // 检查登录状态
  const isLoggedIn = await checkLoginStatus()

  if (!isLoggedIn) {
    ElMessage.warning('请先登录')
    next('/login')
  } else {
    next()
  }
})

export default router
